package com.itheima.controller;

import com.itheima.pojo.Emp;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.pojo.empQueryParam;
import com.itheima.service.EmpService;
import com.itheima.service.impl.EmpServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpContoller {

    @Autowired
    private EmpService empService;

    //返回所有员工信息

    /**
     * 查询员工信息
     */
    @GetMapping()
    public Result getAllEmps(empQueryParam empQueryParam)
    {
//        Long  total=empServiceimpl.getCount();
//        List<Emp> emps=empServiceimpl.getEmps(  page,pageSize);
        log.info("page:{} pageSize:{}",empQueryParam);
        PageResult<Emp> pageResult=empService.getEmpByPage(empQueryParam);
        return Result.success(pageResult);
    }

    /**
     * 增加员工信息
     */
    @PostMapping()
    public Result addEmp(@RequestBody Emp emp)
    {
        log.info("emp:{}",emp);
        //添加员工
        empService.insert(emp);
        return Result.success();
    }

    /**
     * 删除员工信息
     */
//数组传递方式  不需要@RequestParam
   // @DeleteMapping
//    public Result deleteEmp(Integer[] ids)
//    {
//        log.info("ids:{}",ids);
//        empService.deleteByIds(ids);
//        return Result.success();
//    }
    @DeleteMapping
    public Result deleteEmp(@RequestParam List<Integer> ids)
    {
        log.info("ids:{}",ids);
        empService.deleteByIds(ids);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getEmpById(@PathVariable("id") Integer id)
    {
        log.info("id:{}",id);
        Emp emp=empService.getEmpById(id);
        return Result.success(emp);
    }
    @PutMapping
    public Result updateEmp(@RequestBody Emp emp)
    {
        log.info("emp:{}",emp);
        empService.update(emp);
        return Result.success();
    }
}
